/**
 * Created by yangfan on 2016/8/15.
 */
define(["business/services/constant"], function (constant) {
    var baseurl2 = constant.config.BASEURL_BOSTBAR;
    var libUrl = constant.config.BASEURL_LIBRARY;
    var videoUrl = constant.config.BASEURL_VIDEO;
    var NewsUlrl =constant.config.BASEURL_NEWS;
    var databaseUrl = constant.config.BASEURL_DATABASE;
    var userUrl= constant.config.BASEURL_USER ;
    var userCommentApi = constant.config.CONSTANT_COMPANYCOMMENT ;
    var userCollectionApi = constant.config.CONSTANT_COLLECTIONA ;

    var CONSTANT_DATALIB = "elink_database/api/file";
    var CONSTANT_MYCOLLECT = "elink_database/api/collection";
    var CONSTANT_COMMENT = "elink_database/api/comment";

    //library
    var postBar = "elink_postbar/api/collection";
    var newBase = "elink_news/api/collection";
    var boster_url = constant.config.BASEURL_BOSTBAR;
    var videoBase = "elink_video/api/videocollection"
    var CONSTANT_BOSTBAR = constant.config.CONSTANT_BOSTBAR;
    var CONSTANT_LIBRARY = "elink_library/api/thesis";
    var CONSTANT_LIBRARY_COMMENT = "elink_library/api/comment";
    var CONSTANT_LIBRARY_COLLECTION = "elink_library/api/collection";
    var CONSTANT_VIDEO = "elink_video/api/course";

    var url_person = constant.config.BASEURL_USER;
    var person_url = constant.config.CONSTANT_PERSON;
    var follow_url = constant.config.CONSTANT_FOLLOW;
    var notifyAPI = constant.config.CONSTANT_NOTIFY;
    var messageAPI = constant.config.CONSTANT_MESSAGE;
    var postbarAPI = constant.config.CONSTANT_BOSTBAR;

    var fileAPI = constant.config.CONSTANT_FILE;
    var libAPI = constant.config.CONSTANT_LIBTHEISE;
    var fileCollectionAPI = constant.config.CONSTANT_FILECOLLECTION;
    var injector = angular.injector(["ng"]);
    var token = "seed="+ localStorage.seed + "&authToken=" + localStorage.authToken;
    var filterRequestion = function(requestion){
        if(requestion.data){
            var str="";
            for(var key in requestion.data){
                if(typeof  requestion.data[key]==="undefined"|| requestion.data[key]===""||   toString.call( requestion.data[key])=="[object Null]" || !requestion.data.hasOwnProperty(key)){
                    //类型错误 或需要被过滤
                }else{
                    str += key +"="+requestion.data[key]+"&"
                }
            }
            str = str.substr(0,str.lastIndexOf("&"));
            return str +"&"+ token;
        }
    };
    var httpSend =function(requestion,baseUrl,constant){
        var url;
        if(requestion.data && requestion.data.length!=0){
            var str = filterRequestion(requestion);
        }
        if(str&&!requestion.uri){
                url = baseUrl+"/"+constant +"?"+str;
        }else if(str&&requestion.uri){
            url = baseUrl+"/"+constant+"/"+requestion.uri+"?"+str;
        }else if(!str&&requestion.uri){
            url = baseUrl+"/"+constant+"/"+requestion.uri
        }
        if(url!=undefined){
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:url
                })
            };
            return  injector.invoke(httpSend)
        }
    };

    return {
        getquerssInfo :function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:baseurl2+"/"+postBar+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getNewsInfo :function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:NewsUlrl+"/"+newBase+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        //我下载的文件 -- 工程资料库
        getMyDownLoadFile:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:databaseUrl+"/"+CONSTANT_DATALIB+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        //我上传的文件 -  工程资料库
        getMyUploadFile :function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:databaseUrl+"/"+CONSTANT_DATALIB+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getLibraryUploadFile :function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:libUrl+"/"+CONSTANT_LIBRARY+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getMyCollect :function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:databaseUrl+"/"+CONSTANT_MYCOLLECT+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getMyLibraryCollect :function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:libUrl+"/"+CONSTANT_LIBRARY_COLLECTION+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getMyvideoCollect :function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:videoUrl+"/"+videoBase+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getMyComment :function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:userUrl+"/"+userCommentApi+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getCommentForMe:function(requestion){
        var str = filterRequestion(requestion);
        var httpSend = function($http){
            return $http({
                method: requestion.type,
                url:userUrl+"/"+userCommentApi+"/"+requestion.uri+"?"+str
            })
        };
        return  injector.invoke(httpSend);
       },
        getMyCourse:function(requestion){
        //我的视频借口
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:videoUrl+"/"+CONSTANT_VIDEO+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        cancelCourseEnroll:function(requestion){
            var str = filterRequestion(requestion);
            var httpSend = function($http){
                return $http({
                    method: requestion.type,
                    url:library_baseUrl+"/"+CONSTANT_VIDEO+"/"+requestion.uri+"?"+str
                })
            };
            return  injector.invoke(httpSend)
        },
        getMyQuestionList:function(requestion){
            return constant.httpSend(boster_url,CONSTANT_BOSTBAR,requestion);
        },
        getMyAnswerList:function(requestion){
            return constant.httpSend(boster_url,CONSTANT_BOSTBAR,requestion);
        },
        myEngServices:function(requestion){
        return constant.httpSend(url_person,person_url,requestion);
        },
        followServices:function(requestion){
            return constant.httpSend(url_person,follow_url,requestion);
        },
        notifyServices:function(requestion){
            return constant.httpSend(url_person,notifyAPI,requestion);
        },
		messageServices:function(requestion){
            return constant.httpSend(url_person,messageAPI,requestion);
        },
        postbarServices:function(requestion){
            return constant.httpSend(baseurl2,postbarAPI,requestion);
        },
        videoServices:function(requestion){
            return constant.httpSend(videoUrl,CONSTANT_VIDEO,requestion);
        },
        fileServices:function(requestion){
            return constant.httpSend(databaseUrl,fileAPI,requestion);
        },
        fileCollectServices:function(requestion){
            return constant.httpSend(databaseUrl,fileCollectionAPI,requestion);
        },
        //获取资料 论文文件详情
        dataServices:function(requestion){
            return constant.httpSend(databaseUrl,fileAPI,requestion);
        },
        libServices:function(requestion){
            return constant.httpSend(libUrl,libAPI,requestion);
        },
        collectServices:function(requestion){
            return constant.httpSend(userUrl,userCollectionApi,requestion);
        },
        userCommentServices:function(requestion){
            return constant.httpSend(userUrl,userCommentApi,requestion);
        },
        personServices:function(requestion){
            return constant.httpSend(userUrl,person_url,requestion);
        },


    }



});

